package xyz.jpcr.covid19app.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.like.LikeButton;
import com.like.OnLikeListener;

import org.eazegraph.lib.charts.PieChart;
import org.eazegraph.lib.models.PieModel;

import java.text.NumberFormat;

import xyz.jpcr.covid19app.R;
import xyz.jpcr.covid19app.Util.IDBHelper;
import xyz.jpcr.covid19app.Util.IDateUtil;
import xyz.jpcr.covid19app.Util.ISendBroadcastUtil;

import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_CONFIRMED;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_CURED;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_CURRENT_CONFIRMED;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_DEAD;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_LAST_UPDATE_TIME;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_NAME;
import static xyz.jpcr.covid19app.Adapter.CityAdapter.CITY_SUSPECTED;

public class PerCityData extends AppCompatActivity {


    Toolbar toolbar;
    TextView per_city_CurrentConfirmed, per_city_confirmed, per_city_suspected, per_city_deaths, per_city_Cured, per_city_clock, per_city_day;
    PieChart per_city_piechart;
    IDBHelper idbHelper;
    LikeButton likeButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_per_city_data);

        toolbar = findViewById(R.id.layoutToolBar);

        idbHelper = new IDBHelper(this);
        likeButton = findViewById(R.id.city_likebtn);
        per_city_CurrentConfirmed = findViewById(R.id.per_city_CurrentConfirmed);
        per_city_confirmed = findViewById(R.id.per_city_confirmed);
        per_city_suspected = findViewById(R.id.per_city_suspected);
        per_city_deaths = findViewById(R.id.per_city_deaths);
        per_city_Cured = findViewById(R.id.per_city_Cured);
        per_city_clock = findViewById(R.id.per_city_clock);
        per_city_day = findViewById(R.id.per_city_day);
        per_city_piechart = findViewById(R.id.per_city_piechart);



        Intent intent = getIntent();
        int current_confirmed = intent.getIntExtra(CITY_CURRENT_CONFIRMED, 0);
        int suspected = intent.getIntExtra(CITY_SUSPECTED, 0);
        int cured = intent.getIntExtra(CITY_CURED, 0);
        int dead = intent.getIntExtra(CITY_DEAD, 0);
        int confirmed = intent.getIntExtra(CITY_CONFIRMED, 0);
        Long updateTime = intent.getLongExtra(CITY_LAST_UPDATE_TIME, 0);
        String city_name = intent.getStringExtra(CITY_NAME);

        if(idbHelper.query(city_name)) {
            likeButton.setLiked(true);
        }

        likeButton.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                if(idbHelper.insert(city_name, current_confirmed, suspected, cured, dead, confirmed, updateTime)) {
                    ISendBroadcastUtil.getInstance().updateWidget(PerCityData.this);
                    Toast.makeText(PerCityData.this, PerCityData.this.getText(R.string.add_success), Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(PerCityData.this, PerCityData.this.getText(R.string.add_failed), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void unLiked(LikeButton likeButton) {
                if(idbHelper.delete(city_name)) {
                    ISendBroadcastUtil.getInstance().updateWidget(PerCityData.this);
                    Toast.makeText(PerCityData.this, PerCityData.this.getText(R.string.cancel_success), Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(PerCityData.this, PerCityData.this.getText(R.string.cancel_failed), Toast.LENGTH_SHORT).show();
                }
            }
        });

        toolbar.setTitle(city_name);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        per_city_CurrentConfirmed.setText(NumberFormat.getInstance().format(current_confirmed));
        per_city_confirmed.setText(NumberFormat.getInstance().format(confirmed));
        per_city_suspected.setText(NumberFormat.getInstance().format(suspected));
        per_city_deaths.setText(NumberFormat.getInstance().format(dead));
        per_city_Cured.setText(NumberFormat.getInstance().format(cured));
        per_city_clock.setText(IDateUtil.getInstance().formatDate(updateTime, 2));
        per_city_day.setText(IDateUtil.getInstance().formatDate(updateTime, 1));

        per_city_piechart.addPieSlice(new PieModel("Current Confirmed", current_confirmed, Color.parseColor("#fbc233")));
        per_city_piechart.addPieSlice(new PieModel("Suspected", suspected, Color.parseColor("#08a045")));
        per_city_piechart.addPieSlice(new PieModel("Deaths", dead, Color.parseColor("#f6404f")));
        per_city_piechart.addPieSlice(new PieModel("Cured", cured, Color.parseColor("#FF03DAC5")));
        per_city_piechart.startAnimation();

    }
}